package cn.apthink.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import org.noear.solon.annotation.Component;
import org.noear.solon.annotation.Inject;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import cn.apthink.model.UParameter;
import cn.apthink.mybatisplus.entity.SystemDataEntity;
import cn.apthink.mybatisplus.entity.SystemParameterEntity;
import cn.apthink.mybatisplus.entity.UserParameterEntity;
import cn.apthink.mybatisplus.service.MenuService;
import cn.apthink.mybatisplus.service.PermissionService;
import cn.apthink.mybatisplus.service.SystemDataService;
import cn.apthink.mybatisplus.service.SystemParameterService;
import cn.apthink.mybatisplus.service.UserParameterService;
import cn.apthink.utility.Util;

@Component
public class SystemService {
	@Inject
	UserParameterService userParameterService;
	@Inject
	SystemParameterService systemParameterService;
	@Inject
	MenuService menuService;
	@Inject
	PermissionService permissionService;
	@Inject
	SystemDataService systemDataService;
	
	public List<SystemDataEntity> getSystemData() {
		return systemDataService.list();
	}

	public String getSystemParameter(String id) {
		SystemParameterEntity e = systemParameterService.getById(id);
		return e==null?null:e.getValue();
	}

	public Map<String, String> getSystemParameter(List<String> ids) {
		List<SystemParameterEntity> list = systemParameterService.list();
		Map<String, String> result=new HashMap<String, String>();
		ids.forEach(x->{
			Optional<SystemParameterEntity> o = list.stream().filter(y->{
				return x.equalsIgnoreCase(y.getId());
			}).findFirst();
			if (o.isPresent()) {
				result.put(x, o.get().getValue());
			} else {
				result.put(x, null);
			}
		});
		return result;
	}

	public String getUserParameter(String userid, String id) {
		QueryWrapper<UserParameterEntity> q = new QueryWrapper<>();
		q.eq("userid", userid).eq("sid", id);
		List<UserParameterEntity> ls = userParameterService.list(q);
		String result;
		if (Util.isEmpty(ls)) {
			SystemParameterEntity e = systemParameterService.getById(id);
			result = e.getValue();
		} else {
			result = ls.get(0).getValue();
		}
		return result;
	}
	
	public Map<String, String> getUserParameter(String userid, List<String> ids) {
		QueryWrapper<UserParameterEntity> q = new QueryWrapper<>();
		q.eq("userid", userid).in("sid", ids);
		List<UserParameterEntity> ls = userParameterService.list(q);
		Map<String, String> result = this.getSystemParameter(ids);;
		if (!Util.isEmpty(ls)) {
			ls.forEach(x->{
				result.put(x.getSid(), x.getValue());
			});;
		}
		return result;
	}
	
	public void saveUserParameter(String userid, UParameter param) {
		QueryWrapper<UserParameterEntity> q = new QueryWrapper<>();
		q.eq("userid", userid).eq("sid", param.getSid());		
		userParameterService.remove(q);
		UserParameterEntity e = new UserParameterEntity();
		e.setUserid(userid);
		e.setSid(param.getSid());
		e.setValue(param.getValue());
		userParameterService.save(e);
	}

	public void saveSystemParameter(UParameter param) {	
		SystemParameterEntity e=systemParameterService.getById(param.getSid());
		e.setValue(param.getValue());
		systemParameterService.save(e);
	}
}
